<script setup>
import { reactive, ref, toRaw } from 'vue'
import { useUserStore } from '@/store/index.js'

const userStore = useUserStore()

const userLoginLoading = ref(false)

// 用户登录表单属性
const userLoginForm = reactive({
  data: {
    username: '',
    password: '',
    rememberpwd: false // 是否记住密码
  }
})

// 用户表单ref
const userLoginFormRef = ref()

const userLoginFormRules = ref({
  username: [{required: true, message: '请输入账号', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}]
})

const submitUserLoginForm = formEl => {
  formEl.validate(valid => {
    if (valid) {
      userLoginLoading.value = true
      userStore.getUserInfo(toRaw(userLoginForm).data)
      userLoginLoading.value = false
    }
  })
}
</script>

<template>
  <div class="login-wrap">
    <el-form label-position="left" :model="userLoginForm.data" :rules="userLoginFormRules" ref="userLoginFormRef"
             label-width="0px" class="login-container">
      <h2 class="title">用户登录</h2>
      <el-form-item prop="username">
        <el-input type="text" size="large" v-model="userLoginForm.data.username" auto-complete="off"
                  placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" size="large" v-model="userLoginForm.data.password" auto-complete="off"
                  placeholder="密码"></el-input>
      </el-form-item>
      <el-checkbox class="remember" v-model="userLoginForm.data.rememberpwd">七天内免登录</el-checkbox>
      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%" @click="submitUserLoginForm(userLoginFormRef)"
                   :loading="userLoginLoading">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="less" scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100vh;
  padding-top: 10%;

  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.remember {
  margin: 0px 0px 35px 0px;
}
</style>
